package org.hq.net.okhttp;

import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;

public class Okhttp {
    String url = "https://www.4f4023.com/video/jingpin/";
    public void getSync() {
        //同步请求
        OkHttpClient httpClient = new OkHttpClient();

        Request getRequest = new Request.Builder().url(url).get().build();
        Request.Builder builder = new Request.Builder().url(url).get();

        //准备好请求的Call对象
        Call call = httpClient.newCall(getRequest);
        try {
            Response response = call.execute();

            System.out.println(response.body().string());
            response.body().close();
            call.clone();
            httpClient.clone();
        } catch (Exception e) {

        }
    }
    public void getAsync() {
        //异步请求
        OkHttpClient httpClient = new OkHttpClient();
       // String url = "https://www.baidu.com/";
        Request getRequest = new Request.Builder()
                .url(url)
                .get()
                .build();
        //准备好请求的Call对象
        Call call = httpClient.newCall(getRequest);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
              System.out.println(response.body().string());
                ResponseBody body = response.body();
                String string = body.string();
                byte[] bytes = body.bytes();
                InputStream inputStream = body.byteStream();
            }
        });
    }
    public void postSync(){//同步请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient=new OkHttpClient();
                FormBody formBody=new FormBody.Builder()
                        .add("a","1")
                        .add("b","2")
                        .build();
                Request request=new Request.Builder()
                        .post(formBody)
                        .url("https://www.httpbin.org/post")
                        .build();
                //准备好请求的Call对象
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    System.out.println(response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public void postAsync(){//异步请求
        OkHttpClient okHttpClient=new OkHttpClient();
        FormBody formBody=new FormBody.Builder()
                .add("a","1")
                .add("b","2")
                .build();
        Request request=new Request.Builder()
                .post(formBody)
                .url("https://www.httpbin.org/post")
                .build();
        //准备好请求的Call对象
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println(response.body().string());
            }
        });
    }










}
